<?php
namespace App\Listener;

use Hyperf\Event\Contract\ListenerInterface;
use Hyperf\Event\Annotation\Listener;

use App\Query\LevelQuery;
use App\Repository\LevelRepository;

/**
 * @Listener 
 */
class UserLevelListener implements ListenerInterface
{
    public function listen(): array
    {
        return [
            \App\Event\User\CreateEvent::class,
            \App\Event\User\MoneyUserEvent::class,
        ];
    }
    
    // 用户升级
    public function process($event)
    {
        // 用户
        $user = $event->user;
        
        // 查询业绩满足
        if($level = LevelQuery::next($user->money_user))
        {
            $level = LevelRepository::make($level);
            
            // 判断等级变动
            if($level->is($user))
            {
                $user->setLevel($level);
            }
        }
        
    }
}